<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form :inline="true" size="small" :model="formInline" class="search_form">
        <el-form-item label="医院名称：">
          <!-- <el-input v-model="formInline.hospitalName" placeholder="医院名称"></el-input> -->
          <el-select v-model="formInline.hospital" filterable clearable>
            <el-option v-for="(item,index) in hospitalList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否出现投诉：">
          <el-select v-model="formInline.hasComplaint" clearable>
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人：">
          <el-select v-model="formInline.principal" clearable>
            <el-option v-for="(item,index) in userList" :key="index" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="回访时间：">
          <el-date-picker v-model="formInline.orderTime" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="客户信息：">
          <el-input v-model="formInline.customerTerm" placeholder="姓名/电话"></el-input>
        </el-form-item>

      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="onSearch()">筛选</el-button>
        <el-button type="default" size="small" @click="rest">清空筛选</el-button>
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button type="primary" size="small" @click="downloadExcel" :loading="dloading">导出筛选结果</el-button>
      </div>
      <!-- 操作区域 end -->

      <el-table ref="elTable" class="el_table" size="small" :data="tableData">
        <el-table-column prop="settleRegisterCnt" label="医院信息" width="240px">
          <template slot-scope="{row}">
            <tab-hospital :user="showFormatter(row)" :show="false"></tab-hospital>
          </template>
        </el-table-column>
        <el-table-column prop="settleRegisterCnt" label="客户信息" width="120px">
          <template slot-scope="{row}">
            <tab-user :user="userFormatter(row)" :city="false"></tab-user>
          </template>
        </el-table-column>
        <el-table-column prop="appointmentTime" label="预约时间" show-overflow-tooltip width="120px">
          <template slot-scope="{row}">{{showTime(row.appointmentTime)}}</template>
        </el-table-column>
        <el-table-column prop="receptionService" label="满意度" show-overflow-tooltip />
        <el-table-column prop="outpatientSurroundings" label="权益" show-overflow-tooltip />
        <el-table-column prop="doctorExpert" label="价格" show-overflow-tooltip />
        <el-table-column prop="other" label="方案" show-overflow-tooltip />
        <el-table-column prop="worry" label="复诊/顾虑" show-overflow-tooltip />
        <el-table-column prop="hasComplaint" label="是否出现投诉" show-overflow-tooltip>
          <template slot-scope="{row}">{{row.hasComplaint == 1?'是':'否'}}</template>
        </el-table-column>
        <el-table-column prop="complaintContent" label="投诉内容" show-overflow-tooltip />
        <el-table-column prop="createdDate" label="回访时间" show-overflow-tooltip width="120px">
          <template slot-scope="{row}">{{showTime(row.createdDate)}}</template>
        </el-table-column>
        <el-table-column prop="principal" label="负责人" show-overflow-tooltip />

      </el-table>
      <div class="pagination_list">
        <el-pagination background layout="sizes, total,prev, pager, next, jumper" :total="pageTotal" :page-size="pageSize" :page-sizes="pageSizes" :current-page.sync="pageNumber" @current-change="getList" @size-change="changePageSize" />
      </div>
    </div>
    <!-- 表格 end -->

  </div>
</template>

<script>
import {
  GETRETURNVISITSEARCH,
  RETURNVISITEXPORT,
  GETUSEROPTIONS,
} from "@/http/system";
import { showToast, showModal, deepClone } from "@/common/util";
import { exportExcel } from "@/common/common";
import GlsxDialog from "@/components/Dialog/index";
import TabUser from "@/components/user";
import TabHospital from "@/components/hospital";
import { GETHOSPITALOPTIONS } from "@/http/hospital";
import moment from "moment";
export default {
  name: "BookPage",
  props: {
    status: {
      type: [String, Number],
      default: "",
    },
  },
  components: {
    GlsxDialog,
    TabUser,
    TabHospital,
  },
  data() {
    return {
      formInline: { orderTime: [] },
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      showDialog: false,
      loading: false,
      dloading: false,
      orderTime: [],
      userList: [],
      hospitalList: [],
    };
  },
  mounted() {
    this.getList();
    this.getUser();
    this.getHospital();
  },
  methods: {
    /** 重置 */
    rest() {
      this.formInline = {};
      this.orderTime = [];
      this.getList();
    },

    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let oParams = deepClone(this.formInline);
      let n = oParams.orderTime;

      if (n && n.length > 0) {
        oParams.rvsDate = n[0];
        oParams.rveDate = n[1];
      }

      delete oParams.orderTime;
      oParams.pageSize = this.pageSize;
      return oParams;
    },

    /** 获取列表 */
    getList(current) {
      let _this = this;
      let oParams = this.getParams();
      oParams.pageNumber = current || this.pageNumber;
      GETRETURNVISITSEARCH(oParams).then((res) => {
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let options = {};

      let o = deepClone(_this.formInline);
      let m = Object.keys(o);

      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有回访记录?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            options.url = RETURNVISITEXPORT;
            options.data = _this.getParams();
            options.fileName = "回访记录.xlsx";
            options.method = "GET";
            _this.dloading = true;
            exportExcel(options)
              .then((res) => {
                _this.dloading = false;
              })
              .catch((error) => {
                _this.dloading = false;
              });
          },
        });
        return false;
      }

      showToast({
        message: "请选择筛选条件后再导出",
      });
    },

    // 获取负责人列表
    getUser(name) {
      GETUSEROPTIONS({ username: name }).then((res) => {
        this.userList = res.data || [];
      });
    },

    // 获取所有医院
    getHospital() {
      let oParams = {};
      GETHOSPITALOPTIONS(oParams).then((res) => {
        this.hospitalList = res.data || [];
      });
    },

    // 格式化用户数据
    showFormatter(row) {
      let o = deepClone(row);
      if (!o) {
        return {};
      }
      o.id = o.hospitalId;
      return o;
    },

    // 格式化展示时间
    showTime(str) {
      if (!str) {
        return "";
      }
      return moment(str).format("yyyy-MM-DD HH:mm");
    },

    // 格式化处理用户信息
    userFormatter(row) {
      let o = deepClone(row);
      o.id = row.clueId;
      o.name = o.customerName
      o.phone = o.customerPhone
      return o;
    },

    // 搜索
    onSearch(){
      this.pageNumber = 1;
      this.getList()
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/style/customer/index.scss";
</style>
